package cn.jeeweb.web.modules.cable.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mvc.controller.BaseBeanController;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.query.annotation.PageableDefaults;
import cn.jeeweb.common.query.data.PropertyPreFilterable;
import cn.jeeweb.common.utils.FastJsonUtils;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.cable.entity.RobotBrand;
import cn.jeeweb.web.modules.cable.service.IRobotBrandService;
import cn.jeeweb.web.utils.PageRequest;
import cn.xjzn.framework_Core.ModuleActionType;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @description:机器人品牌
 */

@RestController
@RequestMapping("/cable/robotBrand")
@ViewPrefix("modules/SysConfig")
@Log(title = "robot_brand")
public class RobotBrandController extends BaseBeanController<RobotBrand> {

    @Autowired
    private IRobotBrandService robotBrandService;


    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "ajaxList", method = {RequestMethod.GET, RequestMethod.POST})
    @PageableDefaults(sort = "id=desc")
    @Log(logType = ModuleActionType.SELECT)
    public void ajaxList(PropertyPreFilterable propertyPreFilterable, HttpServletRequest request, HttpServletResponse response) throws IOException {
        EntityWrapper<RobotBrand> entityWrapper = new EntityWrapper<>(entityClass);
        String brandName = request.getParameter("brandName");
        if (!StringUtils.isEmpty(brandName)) {
            entityWrapper.like("brandName", brandName);
        }
        Page pageBean = robotBrandService.selectPage(PageRequest.getPage(), entityWrapper);
        FastJsonUtils.print(pageBean);
    }

    @PostMapping("add")
    @Log(logType = ModuleActionType.INSERT)
    public Response add(RobotBrand entity, BindingResult result,
                        HttpServletRequest request, HttpServletResponse response) {
        // 验证错误
        this.checkError(entity, result);
        EntityWrapper<RobotBrand> entityWrapper = new EntityWrapper<>(RobotBrand.class);
        entityWrapper.eq("brandName", entity.getBrandName());
        RobotBrand old = robotBrandService.selectOne(entityWrapper);
        if (old != null) {
            return Response.error("保存失败，机器品牌已存在");
        } else {
            robotBrandService.insert(entity);
            return Response.ok("添加成功");
        }
    }


    @PostMapping("{id}/update")
    @Log(logType = ModuleActionType.UPDATE)
    public Response update(RobotBrand entity, BindingResult result,
                           HttpServletRequest request, HttpServletResponse response) {
        // 验证错误
        this.checkError(entity, result);
        EntityWrapper<RobotBrand> entityWrapper = new EntityWrapper<>(RobotBrand.class);
        entityWrapper.eq("brandName", entity.getBrandName());
        RobotBrand old = robotBrandService.selectOne(entityWrapper);
        if (old != null && !old.getId().equals(entity.getId())) {
            return Response.error("保存失败，机器品牌已存在");
        } else {
            robotBrandService.insertOrUpdate(entity);
            return Response.ok("更新成功");
        }
    }

    @PostMapping("{id}/delete")
    @Log(logType = ModuleActionType.DELETE)
    public Response delete(@PathVariable("id") String id) {
        robotBrandService.deleteById(id);
        return Response.ok("删除成功");
    }

    @PostMapping("batch/delete")
    @Log(logType = ModuleActionType.DELETE)
    public Response batchDelete(@RequestParam("ids") String[] ids) {
        List<String> idList = java.util.Arrays.asList(ids);
        robotBrandService.deleteBatchIds(idList);
        return Response.ok("删除成功");
    }

    @RequestMapping(value = "getRobotBrandList", method = {RequestMethod.GET, RequestMethod.POST})
    @Log(logType = ModuleActionType.SELECT)
    public void getRobotBrandList() {
        EntityWrapper<RobotBrand> entityWrapper = new EntityWrapper<>(RobotBrand.class);
        List<RobotBrand> robotBrands = robotBrandService.selectList(entityWrapper);
        FastJsonUtils.print(robotBrands);
    }

}